package edu.gatech.cs2340.tigershroud;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class CustomAdapter extends ArrayAdapter<Task> 
{
	private List<Task> list;
	private Activity context;

	public CustomAdapter(Activity context, List<Task> list) 
	{
		super(context, R.layout.customlist, list);
		this.context = context;
		this.list = list;
	}

	public class ViewHolder 
	{
		protected TextView tname, cat, date;
		protected CheckBox status;
		protected Button menu;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		View view = null;
		if(convertView == null) 
		{
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.customlist, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.tname = (TextView) view.findViewById(R.id.tvtname);
			viewHolder.cat = (TextView) view.findViewById(R.id.tvcat);
			viewHolder.date = (TextView) view.findViewById(R.id.tvdate);
			viewHolder.status = (CheckBox) view.findViewById(R.id.cbstatus);
			viewHolder.menu = (Button) view.findViewById(R.id.bmenu);
			
			viewHolder.status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
			{
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
				{
					Task t = (Task) viewHolder.status.getTag();
					t.setSelected(buttonView.isChecked());
				}
			});
			
			viewHolder.menu.setOnClickListener(new View.OnClickListener() 
	        {
	    	   public void onClick(View v) 
	    	   {
	    		   Log.d("CUSTOM", "c");
	    	   }
	        });
			
			view.setTag(viewHolder);
			viewHolder.status.setTag(list.get(position));
		}
		else 
		{
			view = convertView;
			((ViewHolder) view.getTag()).status.setTag(list.get(position));
		}
		
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.tname.setText(list.get(position).getTaskName());
		holder.cat.setText(list.get(position).getCategory());
		holder.date.setText(list.get(position).getDate());
		holder.status.setChecked(list.get(position).isSelected());
		return view;
	}	
}
